package com.chzc.fm.duotin.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chzc.fm.duotin.R;

/**
 * Created by BillSinla on 2016/6/23.
 */
public class TitleBar extends RelativeLayout implements View.OnClickListener{
    private ImageView imageLeft, imageRight;    //左右的图片
    private TextView textSearch, textTitle;     //中间的搜索或者普通的标题
    private AnimationDrawable animation;        //播放菜单
    private mTitleBarListener leftListener, rightListener, searchListener, titleListener;

    private TextView textRight;

    public TitleBar(Context context) {
        super(context);
        initView(context);
        initEvent();
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initEvent();
    }

    private void initView(Context context) {
        inflate(context, R.layout.include_title_bar, this);
        imageLeft = (ImageView) findViewById(R.id.action_bar_left_iv);
        imageRight = (ImageView) findViewById(R.id.action_bar_right_iv);
        textSearch = (TextView) findViewById(R.id.action_bar_search_et);
        textTitle = (TextView) findViewById(R.id.action_bar_title_et);
        textRight = (TextView) findViewById(R.id.action_bar_right_tv);
    }

    private void initEvent() {
        imageLeft.setOnClickListener(this);
        imageRight.setOnClickListener(this);
        textSearch.setOnClickListener(this);
        textTitle.setOnClickListener(this);
        textRight.setOnClickListener(this);
    }

    /**
     * 设置左边Image
     * @param visibility
     * @param resId
     */
    public void setLeft(boolean visibility, int resId){
        if(visibility){
            imageLeft.setVisibility(VISIBLE);
            if(resId != 0) {
                imageLeft.setImageResource(resId);
            }
        }else{
            imageLeft.setVisibility(INVISIBLE);
        }
    }

    /**
     * 设置右边Image
     * @param visibility
     */
    public void setRight(boolean visibility){
        if(visibility){
            imageRight.setVisibility(VISIBLE);
            imageRight.setImageResource(R.drawable.anim_playing_light);
            animation = (AnimationDrawable) imageRight.getDrawable();
            animation.start();
        }else{
            imageRight.setVisibility(INVISIBLE);
            if(animation != null){
                animation.stop();
            }
        }
    }

    public void setRightText(String text){
        imageRight.setVisibility(GONE);
        textRight.setText(text);
        textRight.setVisibility(VISIBLE);
    }

    /**
     * 设置中间bar是否显示
     * @param visibility
     */
    public void setSearchBarVisibilty(boolean visibility){
        if (visibility){
            textSearch.setVisibility(VISIBLE);
        }else {
            textSearch.setVisibility(INVISIBLE);
        }
    }

    public void setTitleBarVisibility(boolean visibility, String str){
        if (visibility){
            textTitle.setVisibility(VISIBLE);
            if(null != str){
                textTitle.setText(str);
            }
        }else {
            textTitle.setVisibility(INVISIBLE);
        }
    }

    public void setLeftListener(mTitleBarListener leftListener) {
        this.leftListener = leftListener;
    }

    public void setRightListener(mTitleBarListener rightListener) {
        this.rightListener = rightListener;
    }

    public void setSearchListener(mTitleBarListener searchListener) {
        this.searchListener = searchListener;
    }

    public void setTitleListener(mTitleBarListener titleListener) {
        this.titleListener = titleListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left_iv:
                if(leftListener != null) {
                    leftListener.onClick(v);
                }
                break;
            case R.id.action_bar_right_iv:
                if(rightListener != null) {
                    rightListener.onClick(v);
                }
                break;
            case R.id.action_bar_search_et:
                if(searchListener != null) {
                    searchListener.onClick(v);
                }
                break;
            case R.id.action_bar_title_et:
                if(titleListener != null) {
                    titleListener.onClick(v);
                }
                break;
            case R.id.action_bar_right_tv:
                if(rightListener != null) {
                    rightListener.onClick(v);
                }
        }
    }

    public interface mTitleBarListener{
        void onClick(View v);
    }
}
